//
//  ANBDProduct.m
//  Ants
//
//  Created by ZHM on 16/9/12.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANBDProduct.h"
#import "ANBDComment.h"
#import "ANBDProductCategory.h"
#import "ANBDUser.h"
#import "NSDate+MJ.h"

@implementation ANBDProduct

- (NSString *)createdAtStr {
    if (!_createdAtStr) {
        //Mon Jan 11 08:47:55 +0800 2016
        //字符串转NSDate
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"EEE MMM d HH:mm:ss Z yyyy";
        //真机需要设置locale
        NSLocale *usLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
        format.locale= usLocale;
        if ([self.createdAt isThisYear]) {
            if ([self.createdAt isToday]) {
                //计算与当前时间的差距
                NSDateComponents *components = [self.createdAt deltaWithNow];
                if (components.hour >= 1) {
                    _createdAtStr = [NSString stringWithFormat:@"%ld小时前",(long)components.hour];
                    return _createdAtStr;
                } else if (components.minute >1){
                    _createdAtStr = [NSString stringWithFormat:@"%ld分钟前",(long)components.minute];
                    return _createdAtStr;
                } else {
                    _createdAtStr = [NSString stringWithFormat:@"%ld秒前",(long)components.second];
                    return _createdAtStr;
                }
            }else if ([self.createdAt isYesterday]){
                format.dateFormat = @"昨天 HH:mm";
                _createdAtStr = [format stringFromDate:self.createdAt];
                return _createdAtStr;
            }else {
                format.dateFormat = @"MM-dd HH:mm";
                _createdAtStr = [format stringFromDate:self.createdAt];
                return _createdAtStr;
            }
        }else {
            format.dateFormat = @"yyyy-MM-dd HH:mm";
            _createdAtStr = [format stringFromDate:self.createdAt];
            return _createdAtStr;
        }
        return @"- - -";
    }
    return _createdAtStr;
}
- (void)setCreatedAtStr:(NSString *)createdAt {
    _createdAtStr = createdAt;
}

@end
